package com.imageim.yimirror.ui.adapter.tendency;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.imageim.yimirror.R;
import com.imageim.yimirror.base.BaseActivity;
import com.imageim.yimirror.bean.FashionBrandBean;
import com.imageim.yimirror.bean.LikeEventInfo;
import com.imageim.yimirror.bean.tendency.TendencyThemeItemBean;
import com.imageim.yimirror.common.event.EventCode;
import com.imageim.yimirror.common.event.EventMessage;
import com.imageim.yimirror.common.network.NetUtil;
import com.imageim.yimirror.common.network.constants.NetConstants;
import com.imageim.yimirror.common.network.listener.NetCallback;
import com.imageim.yimirror.common.network.tool.ParamCreator;
import com.imageim.yimirror.common.ui.GlideUtils;
import com.imageim.yimirror.common.ui.tip.TipUtil;
import com.imageim.yimirror.common.util.UserUtil;
import org.apache.http.message.BasicNameValuePair;
import org.byteam.superadapter.IMulItemViewType;
import org.byteam.superadapter.SuperAdapter;
import org.byteam.superadapter.SuperViewHolder;
import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by Bruce.Zhou on 2018/12/26 09:41.
 * Email: 907160968@qq.com
 */
public class ThemeItemAdapter extends SuperAdapter<TendencyThemeItemBean> {

    public ThemeItemAdapter(Context context, List<TendencyThemeItemBean> items, int layoutResId) {
        super(context, items, layoutResId);
    }

    public ThemeItemAdapter(Context context, List<TendencyThemeItemBean> items, IMulItemViewType<TendencyThemeItemBean> mulItemViewType) {
        super(context, items, mulItemViewType);
    }

    @Override
    public void onBind(SuperViewHolder holder, int viewType, int layoutPosition, TendencyThemeItemBean item) {
        holder.setOnClickListener(R.id.panel_like, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageView ivLike = holder.findViewById(R.id.iv_small_like);
                if (item.getLikeFlag() == 1) {
                    likeOrCancel(layoutPosition, ivLike, 0);
                } else {
                    likeOrCancel(layoutPosition, ivLike, 1);
                }
            }
        });
        holder.setText(R.id.tv_name, item.getTitle());
        holder.setText(R.id.tv_from, item.getSourceUrl());
        if (!TextUtils.isEmpty(item.getPhotoUrl())) {
            ImageView ivPhoto = holder.findViewById(R.id.iv_photo);
            GlideUtils.display(ivPhoto, item.getPhotoUrl());
        }
        ImageView ivLike = holder.findViewById(R.id.iv_small_like);
        if (item.getLikeFlag() == 1) {
            ivLike.setSelected(true);
        } else {
            ivLike.setSelected(false);
        }
    }

    private void likeOrCancel(int position, ImageView ivLike, int like) {
        TendencyThemeItemBean bean = getData().get(position);
        List<BasicNameValuePair> params = new ParamCreator()
                .add("photoNo", UserUtil.getPhotoNo())
                .add("id", bean.getContentId())
                .add("recommendType", FashionBrandBean.getFashionBrandBean().getRecommendType() + "")
                .add("operateType", "" + like).build();
        NetUtil.postObject(getContext(), NetConstants.URL.SET_LIKE_TO_TENDENCY_PHOTO, params, new NetCallback<String>() {
            @Override
            public void onStart() {
                ((BaseActivity)getContext()).showLoading();
            }

            @Override
            public void onSuccess(String result) {
                ((BaseActivity)getContext()).hideLoading();
                bean.setLikeFlag(like);
                if (like == 1) {
                    ivLike.setSelected(true);
                } else {
                    ivLike.setSelected(false);
                }

                EventBus.getDefault().post(new EventMessage(EventCode.CODE_REFRESH_LIKE_NUM, new LikeEventInfo(FashionBrandBean
                        .getFashionBrandBean().getId(), like)));
            }

            @Override
            public void onFail(int errorCode, String errorMsg) {
                TipUtil.show(errorMsg + errorCode);
                ((BaseActivity)getContext()).hideLoading();
            }
        });
    }

}
